package observer;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

@SuppressWarnings("serial")
public class ValoareCurentaHistograma extends Canvas implements Observer, ComponentListener{
	
	//Notifica obiectele atunci cand se face o schimbare
	public void upDate(Subject s) {
		for(int i=0;i<count-1;i++){
			valori[i]=valori[i+1];
		}
		valori[count-1]=s.getVal();
		repaint();
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		count = getWidth();
		valori = new int[count];
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		count = getWidth();
		valori = new int[count];
	}

	private int count = 100;
	private int[] valori = new int [count];
	
	public ValoareCurentaHistograma(){
		addComponentListener(this);
	}
	
	//Updatarea interfetei grafice
	public void update(Graphics g){
		super.update(g);
	}
	
	//Deseneaza obiectele
	public void paint(Graphics g){
		int w=1;
		for(int i=0;i<count;i++){
			g.fillRect(0+w*i, 0, w, valori[i]);
		}
	}
}
